package nl.project.dataImport;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;

public class AddNewEmployeeAndTeacher extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		if (req.getParameter("add") != null) {
			DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
			Key employeeKey = KeyFactory.createKey("Employees", "Employees");
			Entity employee = new Entity("Employee", employeeKey);
			employee.setProperty("employeeId", "100");
			employee.setProperty("password", "passwordhoi");
			employee.setProperty("title", "mw.");
			employee.setProperty("initials", "D.");
			employee.setProperty("prefixes", "");
			employee.setProperty("surName", "Duizinga");
			resp.getWriter().println(employee.toString());
			ds.put(employee);
			employee = new Entity("Employee", employeeKey);
			employee.setProperty("employeeId", "101");
			employee.setProperty("password", "passworddoei");
			employee.setProperty("title", "dhr.");
			employee.setProperty("initials", "P.");
			employee.setProperty("prefixes", "de");
			employee.setProperty("surName", "Berg");
			resp.getWriter().println(employee.toString());
			ds.put(employee);
			Key teacherKey = KeyFactory.createKey("Teachers", "Teachers");
			employee = new Entity("Teacher", teacherKey);
			employee.setProperty("teacherId", "100");
			employee.setProperty("password", "passwordhoi");
			employee.setProperty("title", "dhr.");
			employee.setProperty("initials", "B.");
			employee.setProperty("prefixes", "de");
			employee.setProperty("surName", "Ronde");
			resp.getWriter().println(employee.toString());
			ds.put(employee);
		} else {
			resp.getWriter().println("<html><body>");
			resp.getWriter().println(" <form action=''><input type='hidden' name='add'><input type='submit'></form>");
			
			DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
			Key employeeKey = KeyFactory.createKey("Employees", "Employees");
			Query query = new Query("Employee", employeeKey);
			List<Entity> employees = ds.prepare(query).asList(FetchOptions.Builder.withDefaults());
			
			List<Key> keys = new ArrayList<Key>();
			for (Entity e : employees) {
			        keys.add(e.getKey());
			}
			//ds.delete(keys);
			
			resp.getWriter().println("Done!");
			
			resp.getWriter().println("</body></html>");
		}
	}
}